<template>
  <!-- 消息配置新增-->
  <el-dialog
    :title="title"
    center
    :visible.sync="openState"
    width="1024px"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <el-form ref="addForm" :model="addForm" :rules="rules" label-width="110px">
      <!--消息配置新增-->
      <el-card class="form-card msgConf-card">
        <div slot="header" class="label-header-msg">
          <span></span>
        </div>
        <el-row>
          <el-col :span="24">
            <el-form-item label="事件类型" prop="eventType">
              <el-select
                v-model="addForm.eventType"
                placeholder="事件类型"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in eventTypeList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="是否启用" prop="isUse">
              <el-select
                v-model="addForm.isUse"
                placeholder="是否启用"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dictMap.ON_OFF_STATUS"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="节假日开关" prop="holidayFlag">
              <el-select
                v-model="addForm.holidayFlag"
                placeholder="节假日开关"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dictMap.HOLIDAYFLAGCODE"
                  :key="dict.value"
                  :label="`${dict.value}-${dict.text}`"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="发送开始时间" prop="sendStartDt">
              <el-time-picker
                style="width: 100%"
                v-model="addForm.sendStartDt"
                value-format="HHmmss"
                type="time"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送结束时间" prop="sendEndDt">
              <el-time-picker
                style="width: 100%"
                v-model="addForm.sendEndDt"
                value-format="HHmmss"
                type="time"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="通知类型" prop="sendType">
              <el-checkbox-group v-model="sendTypeList">
                <el-checkbox
                  v-for="dict in dictMap.MSG_SEND_TYPE"
                  :key="dict.value"
                  :label="dict.value"
                  :value="dict.value"
                  @change="sendTypeChange"
                  >{{ dict.text }}
                </el-checkbox
                >
              </el-checkbox-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="每日执行次数" prop="execTime">
              <el-select
                v-model="addForm.execTime"
                placeholder="每日执行次数"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in dailyExecTimesList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件接收人小组" prop="mailRecGroup" v-if="mailRecGroupShow">
              <el-select
                multiple
                v-model="mailRecGroupList"
                placeholder="邮件接收人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件抄送人小组" prop="mailCopyGroup" v-if="mailCopyGroupShow">
              <el-select
                multiple
                v-model="mailCopyGroupList"
                placeholder="邮件抄送人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信接收人小组" prop="smsRecGroup" v-if="smsRecGroupShow">
              <el-select
                multiple
                v-model="smsRecGroupList"
                placeholder="短信接收人小组"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in msgGroupList"
                  :key="dict.value"
                  :label="dict.text"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件主题" prop="mailTopic" v-if="mailTopicShow">
              <el-input
                v-model="addForm.mailTopic"
                placeholder="邮件主题"
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="邮件内容" prop="mailContent" v-if="mailContentShow">
              <el-input
                v-model="addForm.mailContent"
                placeholder="邮件内容"
                type="textarea"
                autosize
              />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item label="短信内容" prop="smsContent" v-if="smsContentShow">
              <el-input
                v-model="addForm.smsContent"
                placeholder="短信内容"
                type="textarea"
                autosize
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-card>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">提 交</el-button>
      <el-button type="primary" @click="reset">重 置</el-button>
      <el-button type="primary" @click="cancel">关 闭</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { msgConfAdd,getMonitorConfList,getMsgGroupList} from "@/api/cnaps/bizMng/msgConf.js";
import { listPageMixin } from "@/libs/listPageMixin";

export default {
  name: "msgConfAddDialog",
  mixins: [listPageMixin],
  components: {

  },
  data() {
    return {
      title: "",
      // 是否显示弹出层
      openState: false,
      mailRecGroupShow: false,
      mailCopyGroupShow: false,
      smsRecGroupShow: false,
      mailTopicShow: false,
      mailContentShow: false,
      smsContentShow: false,
      sendTypeList: [],
      eventTypeList: [],
      msgGroupList: [],
      mailRecGroupList: [],
      mailCopyGroupList: [],
      smsRecGroupList: [],
      dailyExecTimesList: [{value : 1, text : "单次"},{value : 2, text : "循环"}],
      // 字段可编辑控制

      //数据字典
      searchFormDictList: [
       "MSG_SEND_TYPE",
       "ON_OFF_STATUS",
       "HOLIDAYFLAGCODE",
      ],
      // 表单参数
      addForm:{},
      // 表单校验
      rules: {
        eventType: [{ required: true, message: "请选择事件类型", trigger: "blur" }],
        isUse: [{ required: true, message: "请选择启用标识", trigger: "blur" }],
        holidayFlag: [{ required: true, message: "请选择节假日开关", trigger: "blur" }],
        sendStartDt: [{ required: true, message: "请选择发送开始时间", trigger: "blur" }],
        sendEndDt: [{ required: true, message: "请选择发送结束时间", trigger: "blur" }],
        sendType: [{ required: true, message: "请选择通知类型", trigger: "blur" }],
        mailRecGroup: [{ required: false, message: "请选择邮件接收人小组", trigger: "blur" }],
        mailCopyGroup: [{ required: false, message: "请选择邮件抄送人小组", trigger: "blur" }],
        smsRecGroup: [{ required: false, message: "请选择短信接收人小组", trigger: "blur" }],
        mailTopic: [{ required: false, message: "请输入邮件主题", trigger: "blur" }],
        mailContent: [{ required: false, message: "请输入邮件内容", trigger: "blur" }],
        smsContent: [{ required: false, message: "请输入短信内容", trigger: "blur" }],
      },
      opButtonList: () => {
        return [
        ];
      },
    };
  },
  mounted() {
    this.initEventTypeList();
    this.initMsgGroupList();
  },
  methods: {
    // 表单重置
    reset() {
      this.resetForm("addForm");
      this.$set(this, "addForm", {});
      this.sendTypeList = [];
      this.mailRecGroupList = [],
      this.mailCopyGroupList = [],
      this.smsRecGroupList = []
      this.sendTypeChange();
    },
    // 关闭回掉
    handleClose(done) {
      this.$confirm("确认关闭？")
        .then(_ => {
          this.reset();
          done();
        })
        .catch(_ => {});
    },
    show() {
      this.title = "消息配置新增";
      this.openState = true;
      this.reset();
      this.sendTypeChange();
    },

    /** TODO提交按钮 */
    submitForm: function() {
      let mailRecGroup = '';
      this.mailRecGroupList.forEach((v) => {
        mailRecGroup = mailRecGroup + v + ",";
      });

      this.addForm.mailRecGroup = mailRecGroup.substring(0,mailRecGroup.length-1);

      let mailCopyGroup = '';
      this.mailCopyGroupList.forEach((v) => {
        mailCopyGroup = mailCopyGroup + v + ",";
      });

      this.addForm.mailCopyGroup = mailCopyGroup.substring(0,mailCopyGroup.length-1);

      let smsRecGroup = '';
      this.smsRecGroupList.forEach((v) => {
        smsRecGroup = smsRecGroup + v + ",";
      });

      this.addForm.smsRecGroup = smsRecGroup.substring(0,smsRecGroup.length-1);

      let sendType = '';
      this.sendTypeList.forEach((v) => {
        sendType = sendType + v + ",";
      });

      this.addForm.sendType = sendType.substring(0,sendType.length-1);
      this.$refs["addForm"].validate(valid => {
        if (valid) {
          this.submit();
        }
      });
    },

    submit() {
      msgConfAdd(this.addForm).then(res => {
        if (res.code === 'S') {
          this.msgSuccess("提交成功");
          this.cancel();
        }
      }).catch(err => {
      });
    },

    cancel() {
      this.openState = false;
      this.reset();
      this.$emit("editComplete", true);
    },

    //初始化获取监控事件列表
    initEventTypeList(){
      getMonitorConfList(this.addForm).then(res => {
        if (res.code === 'S') {
          const arr = [];
          const list = (res.result) || [];
          list.length &&list.forEach((v) => {
            arr.push({
              value: v.monitorId,
              text: v.eventNm,
            });
          });
          this.eventTypeList = arr;
        }
      });
    },

    //初始化获取消息分组
    initMsgGroupList(){
      getMsgGroupList(this.addForm).then(res => {
        if (res.code === 'S') {
          const arr = [];
          const list = (res.result) || [];
          list.length &&list.forEach((v) => {
            arr.push({
              value: v.groupId,
              text: v.groupNm,
            });
          });
          this.msgGroupList = arr;
        }
      });
    },

    sendTypeChange() {
      const sendType = this.sendTypeList;

      if(sendType.indexOf("01")>-1&sendType.indexOf("02")>-1){//邮件通知和短信通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.rules.mailRecGroup= {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else if(sendType.indexOf("01")>-1&sendType.indexOf("02")<0){//邮件通知
        this.mailRecGroupShow = true;
        this.mailCopyGroupShow = true;
        this.mailTopicShow = true;
        this.mailContentShow = true;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.$set(this.addForm, "smsRecGroup", "");
        this.$set(this.addForm, "smsContent", "");

        this.rules.mailRecGroup= {
          required: true,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: true,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: true,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: true,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else if(sendType.indexOf("02")>-1&sendType.indexOf("01")<0){//短信通知
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = true;
        this.smsContentShow = true;

        this.$set(this.addForm, "mailRecGroup", "");
        this.$set(this.addForm, "mailCopyGroup", "");
        this.$set(this.addForm, "mailTopic", "");
        this.$set(this.addForm, "mailContent", "");

        this.rules.mailRecGroup= {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: true,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: true,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }else{//都不包含
        this.mailRecGroupShow = false;
        this.mailCopyGroupShow = false;
        this.mailTopicShow = false;
        this.mailContentShow = false;
        this.smsRecGroupShow = false;
        this.smsContentShow = false;

        this.$set(this.addForm, "mailRecGroup", "");
        this.$set(this.addForm, "mailCopyGroup", "");
        this.$set(this.addForm, "mailTopic", "");
        this.$set(this.addForm, "mailContent", "");
        this.$set(this.addForm, "smsRecGroup", "");
        this.$set(this.addForm, "smsContent", "");

        this.rules.mailRecGroup= {
          required: false,
          message: "请选择邮件接收人小组",
          trigger: "blur",
        }
        this.rules.mailCopyGroup= {
          required: false,
          message: "请选择邮件抄送人小组",
          trigger: "blur",
        }
        this.rules.mailTopic= {
          required: false,
          message: "请输入邮件主题",
          trigger: "blur",
        }
        this.rules.mailContent= {
          required: false,
          message: "请输入邮件内容",
          trigger: "blur",
        }
        this.rules.smsRecGroup= {
          required: false,
          message: "请选择短信接收人小组",
          trigger: "blur",
        }
        this.rules.smsContent= {
          required: false,
          message: "请输入短信内容",
          trigger: "blur",
        }
      }
    }
  },
};
</script>
<style>
.label-header-msg {
  font-weight: bold;
}

.form-card {
  margin-bottom: 10px;
}
</style>
